package com.lenovo.smarttraffic.wjy;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.ui.activity.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class wjy_lxzs extends BaseActivity {

    private GridView lxzsgridview;



    List<lxdata> lxdata = new ArrayList<>();
    int sightId=0;
    int[] image = new int[]{R.mipmap.response,R.mipmap.response2,R.mipmap.response3,R.mipmap.response4,R.mipmap.response5,R.mipmap.response6,R.mipmap.response8,R.mipmap.response8,R.mipmap.response9};
    MyAdapter adapter = new MyAdapter();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initToolBar(findViewById(R.id.toolbar),true,"旅行助手");
        lxzsgridview = (GridView) findViewById(R.id.lxzsgridview);
        initdata();
        lxzsgridview.setAdapter(adapter);

    }

    private void initdata() {

        Retrofit retrofit = new Retrofit.Builder().baseUrl("http://192.168.1.110:8088/transportservice/action/").addConverterFactory(GsonConverterFactory.create()).build();
        Interface_Sight interface_sight = retrofit.create(Interface_Sight.class);
        Call<Bean_Sightdata> user1 = interface_sight.getCall(new Bean_User1("user1"));
        user1.enqueue(new Callback<Bean_Sightdata>() {
            @Override
            public void onResponse(Call<Bean_Sightdata> call, Response<Bean_Sightdata> response) {
                if (response.code()==200){
                    List<Bean_Sightdata.ROWSDETAILBean> rows_detail = response.body().getROWS_DETAIL();
                    for (int i = 0; i <rows_detail.size() ; i++) {
                        int id = rows_detail.get(i).getId();

                        String name = rows_detail.get(i).getName();
                        int ticket = rows_detail.get(i).getTicket();
                        lxdata.add(new lxdata(id,image[i],name,ticket+""));

                    }
                }
            }

            @Override
            public void onFailure(Call<Bean_Sightdata> call, Throwable t) {
                Toast.makeText(wjy_lxzs.this,"error",Toast.LENGTH_SHORT).show();
            }
        });


    }

    @Override
    protected int getLayout() {
        return R.layout.activity_wjy_lxzs;
    }
    public class MyAdapter extends BaseAdapter{
        @Override
        public int getCount() {
            return lxdata.size();
        }

        @Override
        public Object getItem(int i) {
            return lxdata.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        private ImageView sightimage;
        private TextView sightName;
        private Button buyBtn;
        private TextView sightPrice;

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            view = getLayoutInflater().inflate(R.layout.wjy_lxzs_item,viewGroup,false);

            sightimage = (ImageView) view.findViewById(R.id.sightimage);
            sightName = (TextView) view.findViewById(R.id.sightName);
            buyBtn = (Button) view.findViewById(R.id.buyBtn);
            sightPrice = (TextView) view.findViewById(R.id.sightPrice);

            sightimage.setImageResource(lxdata.get(i).getSightimage());
            sightName.setText(lxdata.get(i).getSightName());
            buyBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    Intent intent = new Intent(wjy_lxzs.this,wjy_lxzs2.class);
                    intent.putExtra("sightId",i);
                    startActivity(intent);
                }
            });
            sightimage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(wjy_lxzs.this,wjy_lxzs3.class);
                    intent.putExtra("lxzs3id",i);
                    startActivity(intent);
                }
            });
            sightPrice.setText(lxdata.get(i).getSightPrice());

            return view;
        }
    }
    public class lxdata{
        int sightId;

        public int getSightId() {
            return sightId;
        }

        public void setSightId(int sightId) {
            this.sightId = sightId;
        }

        int sightimage;
        String sightName;
        String sightPrice;

        public int getSightimage() {
            return sightimage;
        }

        public void setSightimage(int sightimage) {
            this.sightimage = sightimage;
        }

        public String getSightName() {
            return sightName;
        }

        public void setSightName(String sightName) {
            this.sightName = sightName;
        }

        public String getSightPrice() {
            return sightPrice;
        }

        public void setSightPrice(String sightPrice) {
            this.sightPrice = sightPrice;
        }

        public lxdata(int sightId, int sightimage, String sightName, String sightPrice) {
            this.sightId = sightId;
            this.sightimage = sightimage;
            this.sightName = sightName;
            this.sightPrice = sightPrice;
        }
    }

}
